package com.chenjun.TxtFileCutter;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FileCutter extends JFrame
{
	private JLabel jLabel;
	private JTextField jTextField1;
	private JTextField jTextField2;
	private JButton jButton1;
	private JTextArea jTextArea1;
	private FileCutterHandler logCutterHandler = new FileCutterHandler();

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -8772081072312920667L;

	/**
	 * 构造方法中设定基本的UI造型 其余的事件之类的操作放在另外一个初始化方法中完成
	 */
	public FileCutter()
	{
		super.setTitle("无题");
	}

	public static void main(String[] args)
	{
		FileCutter cutter = new FileCutter();
		cutter.init();
		cutter.setUIStyle();
	}

	public void init()
	{
		this.jLabel = new JLabel("要处理的文件拖拽到下面文本框中");
		Font font1 = new Font("微软雅黑", Font.ITALIC, 18);
		this.jLabel.setFont(font1);

		Font font2 = new Font("微软雅黑", Font.BOLD, 14);
		this.jTextField1 = new JTextField();
		jTextField1.setFont(font2);

		Font font3 = new Font("微软雅黑", Font.PLAIN, 11);
		this.jTextField2 = new JTextField("切割成多少份?默认3");
		jTextField2.setFont(font3);

		this.jTextArea1 = new JTextArea();
		jTextArea1.setFont(font3);

		jButton1 = new JButton("走起");
		Font font4 = new Font("微软雅黑", Font.PLAIN, 14);
		jButton1.setFont(font4);

		this.addWindowListener(new WindowAdapter()
		{
			// 关闭窗口回调
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
				super.windowClosing(e);
			}
		});

		/**
		 * 文件拖拽响应回调方法
		 */
		this.jTextField1.setTransferHandler(new TransferHandler()
		{
			/**
			 * 4197337557120171779L
			 */
			private static final long serialVersionUID = -4197337557120171779L;

			@Override
			public boolean importData(JComponent comp, Transferable t)
			{
				try
				{
					Object o = t.getTransferData(DataFlavor.javaFileListFlavor);
					String filepath = o.toString();
					if (filepath.startsWith("["))
					{
						filepath = filepath.substring(1);
					}
					if (filepath.endsWith("]"))
					{
						filepath = filepath.substring(0, filepath.length() - 1);
					}
					System.out.println(filepath);
					jTextField1.setText(filepath);
					return true;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				return false;
			}

			@Override
			public boolean canImport(JComponent comp, DataFlavor[] flavors)
			{
				for (int i = 0; i < flavors.length; i++)
				{
					if (DataFlavor.javaFileListFlavor.equals(flavors[i]))
					{
						return true;
					}
				}
				return false;
			}
		});

		this.jTextField2.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				String txt = jTextField2.getText();
				if (!isNumeric(txt))
				{
					jTextField2.setText("");
				}
			}
		});

		this.jButton1.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				// 注意这里是匿名内部类的this
				letsGo(e, this);
			}
		});
	}

	/**
	 * 传入事件对象以及匿名内部类的实例句柄
	 * 
	 * @param e
	 * @param mouseListener
	 */
	private void letsGo(MouseEvent e, MouseListener mouseListener)
	{
		// TODO
		// 移除鼠标事件监听器
		this.jButton1.removeMouseListener(mouseListener);
		// 阻塞式执行切割
		System.out.println("执行切割");

		String fullPathAndName = jTextField1.getText();
		if (fullPathAndName == null || fullPathAndName.length() == 0)
		{
			JOptionPane.showMessageDialog(null, "你在逗我？文件没拖进去", "提示", JOptionPane.QUESTION_MESSAGE);
			// 还原事件监听器
			this.jButton1.addMouseListener(mouseListener);
			return;
		}
		int count = 3;
		String countStr = jTextField2.getText();
		try
		{
			count = Integer.parseInt(countStr);
		}
		catch (NumberFormatException exception)
		{
			JOptionPane.showMessageDialog(null, "你到底要切多少份?", "提示", JOptionPane.PLAIN_MESSAGE);
			// 还原事件监听器
			this.jButton1.addMouseListener(mouseListener);
			return;
		}
		if (count <= 0)
		{
			JOptionPane.showMessageDialog(null, "你在逗我? 份数要大于0", "提示", JOptionPane.PLAIN_MESSAGE);
			// 还原事件监听器
			this.jButton1.addMouseListener(mouseListener);
			return;
		}
		logCutterHandler.execute(jTextField1.getText(), count);
		JOptionPane.showMessageDialog(null, "搞定了,点我收货吧", "提示", JOptionPane.PLAIN_MESSAGE);
		// 还原事件监听器
		this.jButton1.addMouseListener(mouseListener);
	}

	private void setUIStyle()
	{
		// 窗口大小
		this.setPreferredSize(new Dimension(400, 270));
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.pack();
		// 窗口居中
		this.setLocationRelativeTo(null);
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}

		GridBagLayout layout = new GridBagLayout();
		this.getContentPane().setLayout(layout);
		GridBagConstraints gridCon1 = new GridBagConstraints(); // gridx—组件的横向坐标；girdy—组件的纵向坐标；

		/**
		 * 标签
		 */
		gridCon1.fill = GridBagConstraints.VERTICAL | GridBagConstraints.HORIZONTAL;
		gridCon1.gridx = 0;
		gridCon1.gridy = 0;
		gridCon1.gridwidth = 0;
		gridCon1.insets = new Insets(5, 5, 5, 5); // 上下左右间距
		this.add(jLabel, gridCon1);

		/**
		 * 文件拖拽textField
		 */
		GridBagConstraints gridCon2 = new GridBagConstraints();
		gridCon2.fill = GridBagConstraints.HORIZONTAL;
		gridCon2.weightx = 0.5;
		gridCon2.gridx = 0;
		gridCon2.gridy = 1;
		gridCon2.gridwidth = 0;
		gridCon2.insets = new Insets(5, 5, 5, 5);
		this.add(jTextField1, gridCon2);

		/**
		 * 输入要切割的份数
		 */
		GridBagConstraints gridCon3 = new GridBagConstraints();
		gridCon3.fill = GridBagConstraints.HORIZONTAL;
		gridCon3.weightx = 0.5;
		gridCon3.gridx = 0;
		gridCon3.gridy = 2;
		gridCon3.gridwidth = 1;
		gridCon3.insets = new Insets(5, 5, 5, 5);
		this.add(jTextField2, gridCon3);

		/**
		 * 按钮
		 */
		GridBagConstraints gridCon4 = new GridBagConstraints();
		gridCon4.fill = GridBagConstraints.HORIZONTAL;
		gridCon4.weightx = 0.5;
		gridCon4.gridx = 4;
		gridCon4.gridy = 2;
		gridCon4.insets = new Insets(5, 10, 5, 10);
		this.add(jButton1, gridCon4);

		super.setVisible(true);
	}

	public boolean isNumeric(String str)
	{
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches())
		{
			return false;
		}
		return true;
	}

}
